/*
 * @(#)InternationalStringTypeTransformer.java
 * Date 2013-09-02
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.registry.transformer.xds;

import java.util.List;
import javax.enterprise.context.ApplicationScoped;

import oasis.names.tc.ebxml_regrep.xsd.rim._3.InternationalStringType;
import oasis.names.tc.ebxml_regrep.xsd.rim._3.LocalizedStringType;

import org.apache.commons.lang3.StringUtils;

import us.hornerscorners.lamppost.model.common.LocalizedString;

import us.hornerscorners.lamppost.transformer.AbstractTransformer;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-09-02
 * @author         Jim Horner
 */
@ApplicationScoped
public class InternationalStringTypeTransformer
        extends AbstractTransformer<List<LocalizedString>, InternationalStringType> {

    /**
     * Constructs ...
     *
     */
    public InternationalStringTypeTransformer() {
        super();
    }

    /**
     * Method description
     *
     *
     * @param str
     *
     * @return
     */
    public static InternationalStringType createInternationalString(LocalizedString str) {

        InternationalStringType result = null;

        if (str != null) {

            result = createInternationalString(str.getValue(), str.getCharset(),
                                               str.getLanguage());
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param str
     *
     * @return
     */
    public static InternationalStringType createInternationalString(String str) {

        return createInternationalString(str, null, null);
    }

    /**
     * Method description
     *
     *
     * @param str
     * @param charset
     * @param lang
     *
     * @return
     */
    public static InternationalStringType createInternationalString(String str,
            String charset, String lang) {

        InternationalStringType result = null;

        if (StringUtils.isNotBlank(str)) {

            result = new InternationalStringType();

            result.getLocalizedString().add(createLocalizedString(str, charset, lang));
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param str
     * @param charset
     * @param lang
     *
     * @return
     */
    private static LocalizedStringType createLocalizedString(String str, String charset,
            String lang) {

        LocalizedStringType result = new LocalizedStringType();

        result.setValue(StringUtils.trimToNull(str));

        result.setCharset(StringUtils.trimToNull(charset));

        result.setLang(StringUtils.trimToNull(lang));

        return result;
    }

    /**
     * Method description
     *
     *
     * @param values
     *
     * @return
     */
    @Override
    public InternationalStringType transform(List<LocalizedString> values) {

        InternationalStringType result = null;

        if (values.isEmpty() == false) {

            result = new InternationalStringType();

            for (LocalizedString value : values) {

                result.getLocalizedString().add(createLocalizedString(value.getValue(),
                        value.getCharset(), value.getLanguage()));
            }
        }

        return result;
    }
}
